package com.keyinong.caizhengting.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.Utility;
import com.keyinong.caizhengting.adapter.HongbaoAdapter;
import com.keyinong.caizhengting.entity.HongbaoEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class UsecouponsActivity extends Activity {
    ArrayList<HongbaoEntity> hongbaolist;
    HttpTools httptools = null;
    private HongbaoAdapter hongbaoadapter;
    private ImageButton ib_back;
    private ListView lv_hongbao;
    private TextView tv_more;
    private String off;
    private String couponid;
    private String token;
    private String orderid;
    private String price;
    private OnItemClickListener itemclicke = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position,
                                long id) {
//			SharedPreferences spCoupons = getSharedPreferences("spCoupons",
//					Context.MODE_PRIVATE);
//			Editor editor = spCoupons.edit();
//			editor.putString("couponid",couponid);
//			editor.putString("off",off);
//			editor.commit();
            orderid = getIntent().getStringExtra("orderid");
            price = getIntent().getStringExtra("price");
            Intent intent = new Intent();
            intent.putExtra("orderid", orderid);
            intent.putExtra("off", hongbaolist.get(position).getOff());
            Log.i("zq", "useoff---" + off);
            intent.putExtra("couponid", hongbaolist.get(position).getId());
            intent.putExtra("price", price);
            setResult(2, intent);
            finish();
//				SharedPreferences sp=getSharedPreferences("spReg", Context.MODE_PRIVATE);
//				token = sp.getString("data", "");
//				if (httptools == null) {
//					httptools = new HttpTools(UsecouponsActivity.this);
//				}
//				httptools.updateCoupon(token,hongbaolist.get(position).getId(),orderid,new JsonHttpResponseHandler() {
//
//					@Override
//					public void onSuccess(int statusCode, Header[] headers,
//							JSONObject response) {
//						super.onSuccess(statusCode, headers, response);
//						try {
//							boolean code = response.getBoolean("code");
//							String msg = response.getString("msg");
//							Log.i("zq", "coupons---"+response.toString());
//							if (code == true) {
//								//						String data=response.getString("data");
////								Toast.makeText(BalancepayActivity.this, msg, Toast.LENGTH_SHORT).show();
////								Intent intent = new Intent();
////								intent.setClass(BalancepayActivity.this, FanqiedingcanActivity.class);
////								startActivity(intent);
//								//										view.setBackgroundResource(R.drawable.shouchang1);
//								//						tv_balance.setText("￥"+data+"元");
//								String off = hongbaolist.get(position).getOff();
//								Intent intent = new Intent();
//								intent.putExtra("orderid", orderid);
//								intent.putExtra("off", off);
//								Log.i("zq", "useoff---"+off);
//								intent.putExtra("couponid", hongbaolist.get(position).getId());
//								intent.putExtra("price", price);
//								setResult(2,intent);
//								finish();
//								Log.i("zq", "useconpons----成功");
//							} else {
//								Toast.makeText(UsecouponsActivity.this, msg, Toast.LENGTH_SHORT).show();
//							}
//						} catch (JSONException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//					}
//
//				});
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_usecoupons);
        lv_hongbao = (ListView) findViewById(R.id.lv_hongbao);
        tv_more = (TextView) findViewById(R.id.tv_more);
        Hongbao();
        lv_hongbao.setOnItemClickListener(itemclicke);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
//				intent.putExtra("orderid", orderid);
                intent.putExtra("off", "0");
                intent.putExtra("couponid", "0");
                setResult(1, intent);
                finish();
            }
        });
    }

    private void Hongbao() {
        hongbaolist = new ArrayList<HongbaoEntity>();
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        String orderid = getIntent().getStringExtra("orderid");
//		Log.i("shi", "token---" + token);
        if (httptools == null) {
            httptools = new HttpTools(UsecouponsActivity.this);
        }
        httptools.getCoupons(token, orderid, new JsonHongbao());
    }

    public class JsonHongbao extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONArray response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("info", response.toString());
                for (int i = 0; i < response.length(); i++) {
                    JSONObject data = response.getJSONObject(i);
                    couponid = data.getString("id");
                    String more = data.getString("more");
                    off = data.getString("off");
                    String starttime = data.getString("starttime");
                    String endtime = data.getString("endtime");
                    String note = data.getString("note");

                    hongbaolist.add(new HongbaoEntity(couponid, off, more, starttime, endtime, note));
                }
                if (response.length() == 0) {
                    tv_more.setVisibility(View.VISIBLE);
                } else {
                    tv_more.setVisibility(View.GONE);
                }
                hongbaoadapter = new HongbaoAdapter(UsecouponsActivity.this, hongbaolist);
                lv_hongbao.setAdapter(hongbaoadapter);
                lv_hongbao.setDivider(null);
                Utility.setListViewHeightBasedOnChildren(lv_hongbao);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}
